package org.basic.sms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.basic.common.core.domain.R;
import org.basic.common.core.validate.AddGroup;
import org.basic.common.core.validate.EditGroup;
import org.basic.common.excel.utils.ExcelUtil;
import org.basic.common.idempotent.annotation.RepeatSubmit;
import org.basic.common.log.annotation.Log;
import org.basic.common.log.enums.BusinessType;
import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.common.web.core.BaseController;
import org.basic.sms.domain.bo.SysSmsTemplateBo;
import org.basic.sms.domain.dto.MessageSendDTO;
import org.basic.sms.domain.dto.SysTemplateChangeStatusDTO;
import org.basic.sms.domain.vo.SysSmsTemplateVo;
import org.basic.sms.service.ISysSmsTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 邮件模板
 *
 * @author Shao Hua
 * @date 2023-07-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sms/template")
public class SysSmsTemplateController extends BaseController {

    private final ISysSmsTemplateService sysSmsTemplateService;

    /**
     * 查询邮件模板列表
     */
    @SaCheckPermission("sms:template:list")
    @GetMapping("/list")
    public TableDataInfo<SysSmsTemplateVo> list(SysSmsTemplateBo bo, PageQuery pageQuery) {
        return sysSmsTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件模板列表
     */
    @SaCheckPermission("sms:template:export")
    @Log(title = "邮件模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSmsTemplateBo bo, HttpServletResponse response) {
        List<SysSmsTemplateVo> list = sysSmsTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件模板", SysSmsTemplateVo.class, response);
    }

    /**
     * 获取邮件模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sms:template:query")
    @GetMapping("/{id}")
    public R<SysSmsTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysSmsTemplateService.queryById(id));
    }

    /**
     * 新增邮件模板
     */
    @SaCheckPermission("sms:template:add")
    @Log(title = "邮件模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSmsTemplateBo bo) {
        return toAjax(sysSmsTemplateService.insertByBo(bo));
    }

    /**
     * 修改邮件模板
     */
    @SaCheckPermission("sms:template:edit")
    @Log(title = "邮件模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSmsTemplateBo bo) {
        return toAjax(sysSmsTemplateService.updateByBo(bo));
    }

    /**
     * 删除邮件模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("sms:template:remove")
    @Log(title = "邮件模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysSmsTemplateService.deleteWithValidByIds(List.of(ids), true));
    }


    @PostMapping("/send/message")
    public R<Void> sendMessage(@Validated @RequestBody MessageSendDTO messageSendDTO){
        sysSmsTemplateService.sendMessage(messageSendDTO);
        return R.ok();
    }

    @PostMapping("/changeStatus")
    public R<Void> changeStatus(@Validated @RequestBody SysTemplateChangeStatusDTO dto){
        sysSmsTemplateService.changeStatus(dto);
        return R.ok();
    }
}
